package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author wswdkl
 * @version 1.0
 * @date 2022/2/11 9:50
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 分页查询供应商列表
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> querySupplierPage(Integer page, Integer rows, String supplierName) {
        return supplierService.querySupplierPage(page,rows,supplierName);
    }

    /**
     * 添加或者修改供应商信息
     * @param supplier
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    public ServiceVO saveSupplier(Supplier supplier) {
        boolean flag = supplierService.saveSupplier(supplier);
        if(flag) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(-1,"请求失败");
        }
    }

    /**
     * 删除供应商信息
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ServiceVO deleteSupplier(String ids) {
        boolean flag = supplierService.deleteSupplier(ids);
        if(flag) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(-1,"请求失败");
        }
    }
}
